package com.ias.train.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @program: train
 * @description: 异常处理类
 * @author: Daniel Shen
 * @create: 2022-04-23 15:26
 */
@RestControllerAdvice
public class MessageExceptionHandler {
    @ResponseBody
    @ExceptionHandler
    public JsonResult handlerCustomizeException(Throwable ex) {
        if (ex instanceof MessageWarnException){
            MessageWarnException messageWarnException = (MessageWarnException) ex;
            return JsonResult.warn(messageWarnException);
        } else if(ex instanceof MessageException) {
            MessageException messageException = (MessageException) ex;
            //JsonResult jsonResult = JsonResult.error(customizeException.getCode(), customizeException.getMessage(), customizeException.getData());
            return JsonResult.error(messageException);
        } else if(ex instanceof Exception) {
            return JsonResult.error((Exception) ex);
        }
        return null;
    }
}
